package com.lyb.spzx.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.lyb.spzx.feign.order.OrderFeignClients;
import com.lyb.spzx.feign.product.ProductFeignClient;
import com.lyb.spzx.model.dto.product.SkuSaleDto;
import com.lyb.spzx.model.entity.order.OrderInfo;
import com.lyb.spzx.model.entity.order.OrderItem;
import com.lyb.spzx.model.entity.pay.PaymentInfo;
import com.lyb.spzx.model.entity.product.Product;
import com.lyb.spzx.pay.mapper.PaymentInfoMapper;
import com.lyb.spzx.pay.service.PaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class PaymentInfoServiceImpl implements PaymentInfoService {

    @Autowired
    PaymentInfoMapper paymentInfoMapper;
    @Autowired
    OrderFeignClients orderFeignClients;

    @Autowired
    ProductFeignClient productFeignClient;

    @Override
    public PaymentInfo savePaymentInfo(String orderNo) {
        //查询支付信息，如果已经存在则不保存
        PaymentInfo paymentInfo = paymentInfoMapper.selectByOrderNo(orderNo);
        if (paymentInfo == null){
            OrderInfo orderInfo = orderFeignClients.getOrderInfoByOrderNo(orderNo).getData();
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setOrderNo(orderNo);
            paymentInfo.setPayType(orderInfo.getPayType());
            paymentInfo.setAmount(orderInfo.getTotalAmount());
            paymentInfo.setPaymentStatus(0);
            StringBuilder content = new StringBuilder();
            for (OrderItem orderItem : orderInfo.getOrderItemList()){
                content.append(orderItem.getSkuName());
            }
            paymentInfo.setContent(content.toString());

            paymentInfoMapper.insert(paymentInfo);
        }
        return paymentInfo;
    }

    @Override
    public void updatePaymentStatus(Map<String, String> paramMap, int payType) {
        PaymentInfo paymentInfo = paymentInfoMapper.selectByOrderNo(paramMap.get("out_trade_no"));
        if (paymentInfo.getPaymentStatus() == 1){
            return;
        }
         //更新支付状态
        paymentInfo.setPaymentStatus(1);
        paymentInfo.setOutTradeNo(paramMap.get("trade_on"));
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(paramMap));
        paymentInfo.setPayType(payType);
        orderFeignClients.updateOrderStatus(paymentInfo.getOrderNo(),payType);
//        paymentInfoMapper.updateById(paymentInfo);

        //更新销量和库存
        OrderInfo orderInfo = orderFeignClients.getOrderInfoByOrderNo(paymentInfo.getOrderNo()).getData();
        List<SkuSaleDto> skuSaleDtos = orderInfo.getOrderItemList().stream().map(orderItem -> {
            SkuSaleDto skuSaleDto = new SkuSaleDto();
            skuSaleDto.setSkuId(orderItem.getSkuId());
            skuSaleDto.setNum(orderItem.getSkuNum());
            return skuSaleDto;
        }).collect(Collectors.toList());
        productFeignClient.updateSkuSaleNum(skuSaleDtos);
    }
}
